#!/usr/bin/env python
# coding=utf-8
# Created by han on 2016/5/8
import pickle,os
while True:
    print('''
        欢迎来到宇宙不完整银行请选择：
      本银行只用户提供注册与登陆功能
        ======================
        1.注册账号
        2.用户登陆
        3.关于作者
        ======================
    ''')

    user_input=input("请输入您的选1"
                     "择：")
    if user_input == '1':
        print('''
             菜单：注册账号
             ==============
             1.注册账号
             2.返回
             ==============
        ''')
        sign_input=input("请输入您的选择：")
        if sign_input=='1':                 #如果是1则进入注册流程
            while True:
                input_user=input("请输入要注册的【用户名】或者输入【b】退出:")
                if input_user=='b':             #如果输入b则退出注册
                    break
                elif ' ' in input_user:
                    print('格式有错误，请连续输入字符：')
                else:
                    if os.path.exists('./normal_user.txt'):                 #如果用户信息存放文件存在
                        with open('./normal_user.txt','rb') as user_file:   #调用pickle对字典进行序列化处理
                            user_dict=pickle.load(user_file)                #读取文件中的字典
                        if input_user in user_dict:                         #判断用户名是否在字典中
                            print("[%s]此账户已经被注册"%(input_user))        #在
                        else:                                               #不在
                            input_pass=input('请输入密码: ')
                            with open('./normal_user.txt','rb') as user_file:       #读取已经有的字典并将用户名密码追加到字典中
                                user_dict=pickle.load(user_file)
                                user_dict[input_user]=input_pass
                                with open('./normal_user.txt','wb') as newuser_list:    #将新追加用户名的字典序列化存入到文件中
                                    pickle.dump(user_dict,newuser_list)
                            with open('./lock_user.txt','rb') as lockr_file:
                                lock_dict=pickle.load(lockr_file)
                                lock_dict[input_user] = 3
                            with open('lock_user.txt','wb') as newlockw_file:
                                pickle.dump(lock_dict,newlockw_file)
                                print('''
                                    ============================
                                        注册成功，请继续您的操作。
                                    ============================
                                        ''')
                                continue
                    else:                                                               #如果用户信息文件不存在
                        d={}
                        count=3
                        with open('./normal_user.txt','wb') as userw_file:           #创建文件并且传入一个空字典进去。
                            pickle.dump(d,userw_file)
                        with open('./normal_user.txt', 'rb') as userr_file:          #读取文件中的字典
                            user_dict=pickle.load(userr_file)
                        with open('./lock_user.txt','wb') as lockw_file:
                            pickle.dump(d,lockw_file)
                        with open('./lock_user.txt', 'rb') as lockr_file:
                            lockr_dict=pickle.load(lockr_file)
                        input_pass = input("请输入注册密码：")
                        user_dict[input_user]=input_pass                            #将用户名密码追加到字典中
                        lockr_dict[input_user]=count
                        with open('./normal_user.txt', 'wb') as userww_file:
                            pickle.dump(user_dict, userww_file)
                        with open('./lock_user.txt','wb') as lockww_file:
                            pickle.dump(lockr_dict,lockww_file)
                        print('''
                                   ============================
                                       注册成功，请继续您的操作。
                                   ============================
                                       ''')
                        continue
        elif sign_input!='1':
            continue
    elif user_input == '2':
        while True:
            print('''
            菜单：登陆菜单
             ==============
             1.用户登陆
             2.返回上层
             ==============
            ''')
            sign2_input=input('请输入您的选择：')
            if sign2_input=='1':
                while True:
                    user_name=input("请输入您的【用户名】或者输入【b】返回到上一层:")
                    if user_name=='b':
                        break
                    else:
<<<<<<< HEAD
                        if os.path.exists('./lock_user.txt'):
                            with open('./lock_user.txt', 'rb') as lock_file:  # 检查这个用户是否已经被锁定
                                lock_dict = pickle.load(lock_file)
                            if user_name in lock_dict:
                                with open('./lock_user.txt', 'rb') as lock_file:  # 检查这个用户是否已经被锁定
                                    lock_dict = pickle.load(lock_file)
                                lock_count=lock_dict[user_name]
                                if lock_count!=0:                           #如果错误次数为不为0则提示输入密码
                                    user_pass=input("请输入密码： ")          #如果没有被锁定输入密码
                                    if os.path.exists('./normal_user.txt'):
                                        with open('./normal_user.txt','rb') as nor_file:
                                            nor_user=pickle.load(nor_file)
                                        if user_name not in nor_user:
                                            print('对不起没有这个用户。')
                                            continue
                                        elif nor_user[user_name]==user_pass:
                                            print('欢迎光临')
                                            lock_dict[user_name]=3
                                            with open('./lock_user.txt','wb') as rlock_file:
                                                pickle.dump(lock_dict,rlock_file)
                                            exit()
                                        else:
                                            print('对不起您的输入有错误，请重新输入。')
                                            lock_dict[user_name]=lock_count-1
                                            with open('./lock_user.txt','wb') as wlock_file:
                                                pickle.dump(lock_dict,wlock_file)
                                            continue
=======
                        lock_txt = open('./lock_user.txt', 'r')
                        # lock_user = []
                        # for lock_name in lock_txt.readlines():    #打开整个文件，并赋值
                        #     lock_list = lock_name.strip('\n')     #去掉换行符
                        #     lock_user.append(lock_list)           #将所有的名字放到list中
                        #     lock_txt.close()
                        # if user_name in lock_user:                #如果用户名已经在lock中
                        lock_count=lock_txt.readlines().count("%s\n"%(user_name))
                        if lock_count==3:
                            print("您的账户已经被锁定")             #不让登陆并返回锁定
                            continue
                        else:
                            count = 3
                            while count > 0:
                                pass_word = input("请输入您的密码：")
                                nor_list=open('./normal_user.txt','r')
                                for login_name in nor_list.readlines():
                                    login_list=login_name.strip('\n')
                                    if login_list.split()[0] == user_name and login_list.split()[1] == pass_word:
                                        print('欢迎您的到来！未来属于你')
                                        nor_list.close()
                                        break
>>>>>>> ea13c5f1eec4d72ad921352b9be0742fe05326ea
                                    else:
                                        print("对不起没有这个用户,请【重新输入】或【注册】")
                                        continue
                                else:
                                    print("您的账户已经被锁定")
                            else:
                                print("对不起没有这个用户，请【重新输入】或【注册】")
                                continue
                        else:
                            print("没有这个用户，请您注册")
                            continue
            if sign2_input!='1':
                break
    elif user_input == '3':
        print('''
            姓名：韩旭
            作业：python13【day 1】

        ''')
        sign3_input=input("返回按【b】")
        if sign3_input=='b':
            continue
    else:
        print("你的输入有错误请重新输入")
        print(user_input)
        continue